package org.summerframework.web.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.summerframework.web.exception.resolver.BindExceptionResolveHandler;
import org.summerframework.web.exception.resolver.UriExposeExceptionResolver;
import org.summerframework.web.view.DefaultMappingJackson2JsonView;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
 * @author 石超
 * @version v1.0.0
 */
@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@AutoConfigureAfter(JacksonAutoConfiguration.class)
public class ViewConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConfigurationProperties(prefix = "spring.exception")
    public UriExposeExceptionResolver uriExposeExceptionResolver() {
        return new UriExposeExceptionResolver();
    }

    @Bean
    @ConditionalOnMissingBean
    public BindExceptionResolveHandler bindExceptionResolveHandler() {
        return new BindExceptionResolveHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public DefaultMappingJackson2JsonView jsonView(ObjectMapper objectMapper) {
        return new DefaultMappingJackson2JsonView(objectMapper);
    }
}
